package com.lagou.rpc.consumer.controller;

import com.lagou.rpc.api.IUserService;
import com.lagou.rpc.consumer.proxy.ClientProxy;
import com.lagou.rpc.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

   private List<String> address = Arrays.asList("127.0.0.1_9999","127.0.0.1_8888","127.0.0.1_7777");

    private int index = 0;
    @RequestMapping("/getUserById/{id}")
    @ResponseBody
    public String getUserById(@PathVariable int id){

        if(index>= address.size()){
            index = 0;
        }
        String str = address.get(index);

        System.out.println(str);

        IUserService userService = (IUserService) ClientProxy.CreateProxy(IUserService.class,str.split("_")[0],Integer.valueOf(str.split("_")[1]));
        User user = userService.getById(id);
        System.out.println(user);
        index++;
        return str+":"+user.toString();
    }
}
